from sklearn.datasets import load_breast_cancer
from sklearn.decomposition import PCA
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

data = load_breast_cancer(True)
x = data[0]
y = data[1]

# PCA
n_dim = 3
dc = PCA(n_dim)
x_dc = dc.fit_transform(x)
cmap = plt.cm.get_cmap('rainbow', n_dim)
fig = plt.figure(figsize=[16, 8])
spr = 1
spc = 2
spn = 0
spn += 1
ax = fig.add_subplot(spr, spc, spn, projection='3d')
ax.scatter3D(x_dc[:, 0], x_dc[:, 1], x_dc[:, 2], s=1, c=y, cmap=cmap)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.grid()

# rebuilt
x_rb = dc.inverse_transform(x_dc)  # x rebuilt
print(x_rb.shape)
spn += 1
ax = fig.add_subplot(spr, spc, spn, projection='3d')
ax.scatter3D(x[:, 0], x[:, 1], x[:, 2], s=1, c='g', label='original')
ax.scatter3D(x_rb[:, 0], x_rb[:, 1], x_rb[:, 2], s=1, c='y', label='rebuilt')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.legend()
ax.grid()

plt.show()
